package com.zl.autocode.service.autocode.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeConfigC;
import com.zl.autocode.common.constant.AutocodeProperties;
import com.zl.autocode.dao.autocode.ZLCfgGenerateMapper;
import com.zl.autocode.dao.autocode.ZlPreviewTemplateMapper;
import com.zl.autocode.dto.autocode.previewTemplate.ReqPreviewTemplateAddDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ReqPreviewTemplateDelDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ReqPreviewTemplateDetailDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ReqPreviewTemplateEditDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ReqPreviewTemplateQueryDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ReqPreviewTemplateStateDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ResPreviewTemplateAddDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ResPreviewTemplateDelDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ResPreviewTemplateDetailDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ResPreviewTemplateEditDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ResPreviewTemplateQueryDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ResPreviewTemplateStateDTO;
import com.zl.autocode.dto.autocode.previewTemplate.ResPreviewTemplateVO2;
import com.zl.autocode.entity.autocode.ZlPreviewTemplate;
import com.zl.autocode.entity.autocode.po.ZlPreviewTemplateVo;
import com.zl.autocode.service.autocode.IPreviewTemplateService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 可视化预览模板
 * @author ZL
 */
@Service("autocodePreviewTemplateService")
public class PreviewTemplateServiceImpl extends AutocodeBaseManager implements IPreviewTemplateService {

    @Autowired
    private ZlPreviewTemplateMapper previewTemplateMapper;
    @Autowired
    private AutocodeProperties autocodeProperties;
    @Autowired
    private ZLCfgGenerateMapper cfgGenerateMapper;
    
    @Override
    public ResPreviewTemplateQueryDTO queryPreviewTemplateList(ReqDTO<ReqPreviewTemplateQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqPreviewTemplateQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResPreviewTemplateQueryDTO resDTO = BeanUtil.copyProperties(page, ResPreviewTemplateQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getTemplateTitle())){
            reqData.setTemplateTitle("%"+reqData.getTemplateTitle()+"%");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlPreviewTemplateVo> list = previewTemplateMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResPreviewTemplateVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResPreviewTemplateDetailDTO queryPreviewTemplateDetail(ReqDTO<ReqPreviewTemplateDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqPreviewTemplateDetailDTO reqData = reqInfo.getReqData();
        ResPreviewTemplateDetailDTO res = new ResPreviewTemplateDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlPreviewTemplate view = previewTemplateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResPreviewTemplateAddDTO insertPreviewTemplateInfo(ReqDTO<ReqPreviewTemplateAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqPreviewTemplateAddDTO reqData = reqInfo.getReqData();
        ResPreviewTemplateAddDTO res = new ResPreviewTemplateAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlPreviewTemplate record = new ZlPreviewTemplate();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setTemplateId(IdUtil.simpleUUID());
        record.setTemplatePath(filePath(autocodeProperties.getAutocodePathMajor(),autocodeProperties.getAutocodePathOther()
                ,new String[]{AutocodeConfigC.PATH_CHILDREN_PREVIEW})+record.getTemplateId()+AutocodeConfigC.TEMPLATE_EXT_FTL);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateId",record.getTemplateId());
        //检查主键是否冲突
        if(previewTemplateMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        previewTemplateMapper.insertSelective(record);
        
        ZlPreviewTemplate keyInfo = previewTemplateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResPreviewTemplateEditDTO updatePreviewTemplateInfo(ReqDTO<ReqPreviewTemplateEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqPreviewTemplateEditDTO reqData = reqInfo.getReqData();
        ResPreviewTemplateEditDTO res = new ResPreviewTemplateEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlPreviewTemplate record = new ZlPreviewTemplate();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        previewTemplateMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateId",record.getTemplateId());
        ZlPreviewTemplate keyInfo = previewTemplateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResPreviewTemplateDelDTO deletePreviewTemplateInfo(ReqDTO<ReqPreviewTemplateDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqPreviewTemplateDelDTO reqData = reqInfo.getReqData();
        ResPreviewTemplateDelDTO res = new ResPreviewTemplateDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        ZlPreviewTemplate template = previewTemplateMapper.selectOneByMap(paramMap);
        if(StringUtils.isNotBlank(template.getTemplatePath())
                && template.getTemplatePath().contains(autocodeProperties.getAutocodePathMajor()) 
                && template.getTemplatePath().contains(autocodeProperties.getAutocodePathTemplate())){
            FileUtil.del(handlerRootPath(template.getTemplatePath()));
        }
        previewTemplateMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResPreviewTemplateStateDTO updatePreviewTemplateState(ReqDTO<ReqPreviewTemplateStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqPreviewTemplateStateDTO reqData = reqInfo.getReqData();
        ResPreviewTemplateStateDTO res = new ResPreviewTemplateStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlPreviewTemplate record = new ZlPreviewTemplate();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        previewTemplateMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateId",record.getTemplateId());
        ZlPreviewTemplate keyInfo = previewTemplateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResPreviewTemplateDelDTO deletePreviewTemplateAndDetail(ReqDTO<ReqPreviewTemplateDelDTO> reqInfo) throws Exception{
        return deletePreviewTemplateInfo(reqInfo);
    }
}